package com.cloud.config.rabbitmq;

import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by qzy on 2017/5/14.
 */
@Service
public class RabbitmqService implements RabbitTemplate.ConfirmCallback {
    @Value("${spring.rabbitmq.topicExchange}")
    private String exchangeTopic;
    @Autowired
    private RabbitMessagingTemplate rabbitMessagingTemplate;

    public void sendMsg(String routingKey, Object object) {
        this.rabbitMessagingTemplate.convertAndSend(exchangeTopic, routingKey, MessageBuilder.withBody(object.toString().getBytes()).build());
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("message ID： "+correlationData.getId());
        if (ack){
            System.out.println("消息消费成功");
        }
    }
}
